package code.rocky.chapter03;

// 建议43：避免对象的浅拷贝
public class Suggest43 {
    public static void main(String[] args) {
        Man man = new Man("父亲");

        Man m1 = new Man("大儿子", man);
        Man m2 = m1.clone();
        m2.setName("二儿子");
        m1.getMan().setName("干爹");
        System.out.println(m1.getName() + " 的父亲是 " + m1.getMan().getName());
        System.out.println(m2.getName() + " 的父亲是 " + m1.getMan().getName());
    }
}

class Man implements Cloneable {

    private String name;

    private Man father;

    public Man(String _name) {
        name = _name;
    }

    public Man(String _name,Man _father) {
        name = _name;
        father = _father;
    }

    @Override
    public Man clone() {
        Man man = null;
        try {
            man =(Man) super.clone();
        }
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return man;
    }

    public void setName(String name) {
        this.name  = name;
    }

    public Man getMan() {
        return father;
    }

    public String getName() {
        return name;
    }
}

